<?php

class Common_DataTable_Analyser_ValueFrequency extends Common_DataTable_Analyser_Abstract
{

    public function analyse(array $options = array())
    {

        $columns = isset($options['columns']) ? $options['columns'] : array();

        $rounding = isset($options['rounding']) ? $options['rounding'] : false;

        $frequencies = array();
        foreach ($columns as $columnKey) {
            $frequencies[$columnKey] = array();
        }

        $data = $this->getDataTable()->toArray();
        
        foreach ($columns as $columnKey) {

            $column = $this->getDataTable()->getColumn($columnKey);

            foreach ($data as $row) {
                if (isset($row[$columnKey])) {

                    $value = $row[$columnKey];
                    if ($rounding) {
                        $value = round($value, 0);
                    }

                    if (!isset($frequencies[$columnKey][$value])) {
                        $frequencies[$columnKey][$value] = 0;
                    }

                    $frequencies[$columnKey][$value]++;
                }
            }

        }

        return $frequencies;
    }

}
